package com.na.onlineservey

import com.na.onlineservey.answeringtype.CreateAnsweringTypeAction
import com.na.onlineservey.answeringtype.DeleteAnsweringTypeAction
import com.na.onlineservey.answeringtype.ListAnsweringTypeAction
import com.na.onlineservey.answeringtype.UpdateAnsweringTypeAction
import com.na.onlineservey.answeringtype.ReadAnsweringTypeAction
import com.na.onlineservey.answeringtype.SearchAnsweringTypeAction

import com.docu.common.Message
import org.springframework.beans.factory.annotation.Autowired
import grails.converters.JSON

class AnsweringTypeController {

    @Autowired
    private CreateAnsweringTypeAction createAnsweringTypeAction
    @Autowired
    private UpdateAnsweringTypeAction updateAnsweringTypeAction
    @Autowired
    private ListAnsweringTypeAction listAnsweringTypeAction
    @Autowired
    private DeleteAnsweringTypeAction deleteAnsweringTypeAction
    @Autowired
    private ReadAnsweringTypeAction readAnsweringTypeAction
    @Autowired
    private SearchAnsweringTypeAction searchAnsweringTypeAction

    static allowedMethods = [create: "POST", update: "POST", delete: "POST"]

    def list = {
        List list = listAnsweringTypeAction.execute(params, null)
        render listAnsweringTypeAction.postCondition(null, list) as JSON
    }

    def show = {
        AnsweringType answeringTypeInstance = new AnsweringType()
        render(template: "show", model: [answeringTypeInstance: answeringTypeInstance])
    }

    def create = {
        AnsweringType answeringTypeInstance = new AnsweringType(params)
        AnsweringType answeringTypeInstanceInstance = createAnsweringTypeAction.preCondition(null, answeringTypeInstance)
        Message message = null
        if (answeringTypeInstanceInstance == null) {
            message = createAnsweringTypeAction.getValidationErrorMessageForUI(answeringTypeInstance)
        } else {
            answeringTypeInstanceInstance = createAnsweringTypeAction.execute(null, answeringTypeInstanceInstance)
            if (answeringTypeInstanceInstance) {
                message = createAnsweringTypeAction.getSuccessMessageForUI(answeringTypeInstanceInstance, createAnsweringTypeAction.SUCCESS_SAVE)
            } else {
                message = createAnsweringTypeAction.getErrorMessageForUI(answeringTypeInstance, createAnsweringTypeAction.FAIL_SAVE)
            }
        }
        render message as JSON
    }

    def edit = {
        render readAnsweringTypeAction.execute(params, null) as JSON
    }

    def update = {
        AnsweringType answeringTypeInstance = new AnsweringType(params)
        Object object = updateAnsweringTypeAction.preCondition(params, null)
        Message message = null
        if (object == false) {
            message = updateAnsweringTypeAction.getValidationErrorMessageForUI(answeringTypeInstance)
        } else {
            int noOfRows = (int) updateAnsweringTypeAction.execute(null, object)
            if (noOfRows > 0) {
                message = updateAnsweringTypeAction.getSuccessMessageForUI(answeringTypeInstance, updateAnsweringTypeAction.SUCCESS_UPDATE)
            } else {
                message = updateAnsweringTypeAction.getErrorMessageForUI(answeringTypeInstance, updateAnsweringTypeAction.FAIL_UPDATE)
            }
        }
        render message as JSON
    }

    def delete = {
//        AnsweringType answeringTypeInstance = new AnsweringType(params)
//        answeringTypeInstance.id = Long.parseLong(params.id)
//        answeringTypeInstance.version = Long.parseLong(params.version)
        Message message = null
        AnsweringType answeringTypeInstance =    deleteAnsweringTypeAction.preCondition(params,null)
        if (answeringTypeInstance) {
            int rowCount = (int) deleteAnsweringTypeAction.execute(null, answeringTypeInstance)
            if (rowCount > 0) {
                message = deleteAnsweringTypeAction.getSuccessMessageForUI(answeringTypeInstance, deleteAnsweringTypeAction.SUCCESS_DELETE)
            } else {
                message = deleteAnsweringTypeAction.getErrorMessageForUI(answeringTypeInstance, deleteAnsweringTypeAction.FAIL_DELETE)
            }
        }
        else {
            message = deleteAnsweringTypeAction.getErrorMessageForUI(answeringTypeInstance, deleteAnsweringTypeAction.ALREADY_DELETED)
        }
        render message as JSON
    }

    def search = {
        AnsweringType answeringTypeInstance = searchAnsweringTypeAction.execute(params.fieldName, params.fieldValue)
        if (answeringTypeInstance) {
            render answeringTypeInstance as JSON
        }
        else {
            render ''
        }

    }
}
